import { Switch, Route, Redirect } from 'react-router-dom'
import { Suspense } from 'react'

const RouterView = (props) => {
    let { RouterList } = props
    let routerlist = RouterList.filter(item => !item.to)
    let redirect = RouterList.filter(item => item.to)[0]

    return (
        <Suspense fallback={<div>loadding......</div>}>
            <Switch>
                {
                    routerlist && routerlist.length ? routerlist.map((item, index) => {
                        return <Route path={item.path} key={index} render={ routerProps => {
                            let Son = item.component
                            
                            if (item.children) {
                                return <Son child={item.children} navlinks={item.children.filter(val => !val.to)} />
                            }

                            return <Son {...routerProps} />
                        }} />
                    }) : <div>暂无数据</div>
                }
                {
                    redirect && <Redirect to={redirect.to} from={redirect.from} />
                }
            </Switch>
        </Suspense>
    )

}

export default RouterView